"""
创建张量
"""

import torch
import numpy as np


def test_basic():
    print("传入数组=========================")
    a = torch.Tensor([[1, 2], [3, 4]])
    print("a: torch.Tensor(data)")
    print(a)
    a = torch.tensor([[1, 2], [3, 4]])
    print("a: torch.tensor(data)")
    print(a)
    print(a.shape)
    print(a.dtype)
    print(a.type())
    print()

    print("传入size 2, 3=========================")
    # 值是内存中随机的值,小写的不行
    a = torch.Tensor(2, 3)
    print("a: torch.Tensor(2,3)")
    print(a)
    print(a.shape)
    print(a.dtype)
    print(a.type())
    print()


def test_common():
    print("""几种常用的tensor""")
    print("ones=========================")
    one = torch.ones(2, 2)
    print(one)
    print(one.dtype)
    print(one.shape)
    print()

    print("zeros=========================")
    zero = torch.zeros(2, 2)
    print(zero)
    print(zero.dtype)
    print(zero.shape)
    print()

    print("eye=========================")
    eye = torch.eye(3, 3, dtype=torch.int)
    print(eye)
    print(eye.dtype)
    print(eye.shape)
    print()

    b = torch.Tensor(2, 3)
    print("zeros_like=========================")
    zero2 = torch.zeros_like(b)
    print(zero2)
    print(zero2.dtype)
    print(zero2.shape)
    print()

    print("ones_like=========================")
    ones2 = torch.ones_like(b, dtype=torch.int8)
    print(ones2)
    print(ones2.dtype)
    print(ones2.shape)
    print()


def test_rand():
    print("""随机""")
    print("rand=========================")
    a = torch.rand(2, 2)
    print(a)
    print(a.shape)
    print(a.dtype)
    print()

    print("normal(mean=0, std=rand(5))=========================")
    # 均值固定为0， 标准差随机生成5个。相当于从5个分布中随机抽取一个值构成输出
    a = torch.normal(mean=0.0, std=torch.rand(5))
    print(a)
    print(a.shape)
    print(a.dtype)

    print("normal(mean=rand(5), std=rand(5))=========================")
    # 均值随机生成5个， 标准差随机生成5个
    a = torch.normal(mean=torch.rand(5), std=torch.rand(5))
    print(a)
    print(a.shape)
    print(a.dtype)
    print()

    print("uniform_=========================")
    # 均值随机生成5个， 标准差随机生成5个
    a = torch.Tensor(2, 2).uniform_(-1, 1)
    print(a)
    print(a.shape)
    print(a.dtype)
    print()


def test_range():
    print("序列")
    print("arange=========================")
    a = torch.arange(0, 11, 3)
    print(a)
    print(a.shape)
    print(a.dtype)
    print(a.type())
    print()

    print("linspace等差=========================")
    a = torch.linspace(0, 11, 3)
    print(a)
    print(a.shape)
    print(a.dtype)
    print(a.type())
    print()

    print("randperm=========================")
    a = torch.randperm(10)
    print(a)
    print(a.shape)
    print(a.dtype)
    print(a.type())
    print()


def test_np():
    print()
    print("""numpy""")
    a = np.eye(2, 3)
    print(a)

    a = torch.eye(3, 4)
    a = a.numpy()
    print("a: tensor.numpy()")
    print(a)


if __name__ == "__main__":
    test_basic()
    test_common()
    test_rand()
    test_range()
    test_np()
